package blatt2;

import java.awt.*;
import java.awt.event.*;

public class BallWorld extends Frame {
	public static void main(String[] args) {
		Color bColor = new Color((float) Math.random(), (float) Math.random(),
				(float) Math.random());
		BallWorld world = new BallWorld(bColor);
		world.setVisible(true);
	}

	private static final int FrameWidth = 600;
	private static final int FrameHeight = 400;
	private MovableBall aBall;

	private BallWorld(Color ballColor) {
		// constructor for new ball world
		// resize our frame
		setSize(FrameWidth, FrameHeight);
		setTitle("Ball World");

		WindowListener wl = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};
		addWindowListener(wl);

		// initialize object data field
		aBall = new MovableBall(10, 15, 10);
		aBall.setColor(ballColor);
		aBall.setMotion(3.0, 6.0);
	}

	public void paint(Graphics g) {
		// first, draw the ball
		aBall.paint(g);
		// then move it slightly
		aBall.move();
		if ((aBall.x() < 0) || (aBall.x() > FrameWidth))
			aBall.setMotion(-aBall.xMotion(), aBall.yMotion());
		if ((aBall.y() < 0) || (aBall.y() > FrameHeight))
			aBall.setMotion(aBall.xMotion(), -aBall.yMotion());
		try {
			Thread.currentThread().sleep(40);
		} catch (InterruptedException e) {
		}
		repaint();

	}
}